define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'apply/index' + location.search,
                    add_url: 'apply/add',
                    edit_url: 'apply/edit',
                    del_url: 'apply/del',
                    multi_url: 'apply/multi',
                    import_url: 'apply/import',
                    table: 'apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'store_id', title: __('Store_id')},
                        {field: 'store.store_name', title: __('所属空间/中心'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user.username', title: __('申请人昵称'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user.tel', title: __('申请人手机号'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'store_name', title: __('Store_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'tel', title: __('Tel'), operate: 'LIKE'},
                        // {field: 'contact_tel', title: __('Contact_tel'), operate: 'LIKE'},
                        // {field: 'bank_idcard', title: __('Bank_idcard'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'idcard_z', title: __('Idcard_z'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'idcard_f', title: __('Idcard_f'), operate: 'LIKE'},
                        // {field: 'id_card_valid_start_time', title: __('Id_card_valid_start_time'), operate: 'LIKE'},
                        // {field: 'id_card_valid_time', title: __('Id_card_valid_time'), operate: 'LIKE'},
                        // {field: 'id_card_arddress', title: __('Id_card_arddress'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'zizhi', title: __('Zizhi'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'zz_num', title: __('Zz_num'), operate: 'LIKE'},
                        // {field: 'merchant_name', title: __('Merchant_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'subject_type', title: __('Subject_type'), searchList: {"1":__('Subject_type 1'),"2":__('Subject_type 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'license_address', title: __('License_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'period_begin', title: __('Period_begin'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'period_end', title: __('Period_end'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'business_time', title: __('Business_time'), operate: 'LIKE'},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'edit_time', title: __('Edit_time'), operate: 'LIKE'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'authen_time', title: __('Authen_time'), operate: 'LIKE'},
                        // {field: 'bank_id', title: __('Bank_id')},
                        // {field: 'bank_province', title: __('Bank_province'), operate: 'LIKE'},
                        // {field: 'bank_area', title: __('Bank_area'), operate: 'LIKE'},
                        // {field: 'bank_no', title: __('Bank_no'), operate: 'LIKE'},
                        // {field: 'account_name', title: __('Account_name'), operate: 'LIKE'},
                        // {field: 'bank_account_type', title: __('Bank_account_type'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                
                            {
                                name: 'edit',
                                text: __('审核'),
                                title: __('审核'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                extend: 'data-area=\'["90%", "80%"]\'',
                                url: 'apply/edit',
                                hidden: function(rows){
                                    if (rows.status == '1') {
                                        return false;
                                    }
                                    return true;
                                },
                            }
                            
                            
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-status", function(){
                var authen=$("#c-status").val();
                // alert(authen);
                if(authen=='3')
                {
                    $("#remark").show();
                }
                else
                {
                    $("#remark").hide();
                }

            });

            //选择空间中心
            $(document).on('change', '#c-type', function () {
                if ($(this).val() == 1) {
                    //中心
                    $(".c-area_ids").show();
                    $('form[role=form]').data('validator').options.ignore = '';
                } else {
                    //空间
                    $(".c-area_ids").hide();
                    $('form[role=form]').data('validator').options.ignore = '#c-area_ids';
                }
            });

            $(document).on('change', '#c-area', function () {
                var type = $("#c-type").val();
                //空间判断所属中心
                if (type == 2) {
                    $.ajax({
                        dataType: 'json',
                        type: 'POST',
                        url: 'ajax/checkCenter',
                        data: {area:$(this).val()},
                        success: function (data) {
                            if (data.code == 201) {
                                $(".c-store_id").remove();
                                //动态追加选择中心select
                                $(".one_sel").after('<div class="form-group c-store_id">\n' +
                                    '        <label class="control-label col-xs-12 col-sm-2">指派空间所属中心:</label>\n' +
                                    '        <div class="col-xs-12 col-sm-8">\n' +
                                    '            <input id="c-pid" data-rule="required" data-source="ajax/getAllstore?type=1"\n' +
                                    '                   class="form-control selectpage"\n' +
                                    '                   name="row[pid]"\n' +
                                    '                   type="text" value="">\n' +
                                    '        </div>\n' +
                                    '    </div>');

                                $("#area_tip").show();
                                $('form[role=form]').data('validator').options.ignore = '';
                                Form.events.selectpage($("#edit-form"));
                            } else {
                                $(".c-store_id").remove();
                                $("#area_tip").hide();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {

                        }
                    });
                }

            });


            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
